M=require("ming_node")

a=M.getObjByFile("input.json")
let g_list=a.tree.filter(u=>!/\.(doc|txt|rar|chm|zip|png|jpg|gif|jpeg|webp)$/.test(u.path)).map(u=>u.path);
function addchildren(node){
    if(node.name=="/"){
        node.children=g_list.filter(u=>!u.includes("/")).map(u=>{
            let o={};
            o.name=u;
            o.value=u;
            addchildren(o);
            return o;
        });
    }else{
        node.children=g_list.filter(u=>u.startsWith(node.value) && u!=node.value).map(u=>{
            let o={};
            o.name=u;
            if(o.name.includes(".")){
                o.name=o.name.substr(o.name.lastIndexOf("/")+1) 
            }
            o.value=u;
            addchildren(o);
            return o;
        });
        if(node.children.length==0){
            return;
        }
    }
}
let root={"name":"/",value:"/",children:[]}
addchildren(root)


M.writeFile("output.json",JSON.stringify(root))
